<script>
import Vue from 'vue';
import { initHttpConfig } from '@/api/luch-request.js';

export default {
	globalData: {
		q: null
	},
	data() {
		return {
			updateManager: null
		};
	},
	onLaunch(options) {
		const vm = this;

		console.log('系统信息', uni.getSystemInfoSync());
		console.log('App Launch options', options);
		console.log('App Launch options.path', options.path);
		console.log('App Launch options.query', options.query);

		// #ifdef MP-ALIPAY
		// 支付宝平台，扫二维码进入小程序，记录下二维码内容
		if (options && options.query && options.query.qrCode) {
			this.globalData.q = options.query.qrCode;
		} else {
			this.globalData.q = null;
		}
		// #endif

		// 初始化HTTP配置
		initHttpConfig('App.vue');

		console.log('小程序冷启动...');

		vm.updateManager = uni.getUpdateManager();

		// #ifdef MP-WEIXIN
		uni.hideTabBar({
			animation: false
		});
		// #endif

		vm.initApp();
	},
	onShow(res) {
		const vm = this;
		console.log('App Show', res);
	},
	onError(error) {
		console.error('App onError', this, error);
	},
	onHide() {
		console.log('App Hide');
	},
	methods: {
		initApp() {
			const vm = this;

			// 小程序版本更新检查
			if (vm.updateManager) {
				vm.updateManager.onCheckForUpdate(function(res) {
					// 请求完新版本信息的回调
					if (res.hasUpdate) {
						console.log('小程序不是最新版本');
					} else {
						console.log('小程序已是最新版本');
					}
				});
			}

			// 小程序更新就绪
			if (vm.updateManager) {
				vm.updateManager.onUpdateReady(function(res) {
					uni.showModal({
						title: '更新提示',
						content: '新版本已经准备好，是否更新应用？',
						success(res) {
							if (res.confirm) {
								vm.updateManager.applyUpdate();
							}
						}
					});
				});
			}

			// 小程序更新失败
			if (vm.updateManager) {
				vm.updateManager.onUpdateFailed(function(res) {
					console.log('更新小程序失败', res);
				});
			}

			uni.getSystemInfo({
				success(res) {
					// #ifndef MP
					Vue.prototype.$statusBarHeight = res.statusBarHeight;
					if (e.platform == 'android') {
						Vue.prototype.$titleBarHeight = 50;
					} else {
						Vue.prototype.$titleBarHeight = 45;
					}
					Vue.prototype.$customNavBarHeight = Vue.prototype.$statusBarHeight + Vue.prototype.$titleBarHeight;
					// #endif

					// #ifdef MP-WEIXIN
					Vue.prototype.$statusBarHeight = res.statusBarHeight;
					let custom = wx.getMenuButtonBoundingClientRect();
					Vue.prototype.$customNavBarHeight = custom.bottom + custom.top - res.statusBarHeight;
					Vue.prototype.$titleBarHeight = Vue.prototype.$customNavBarHeight - Vue.prototype.$statusBarHeight;
					// #endif

					// #ifdef MP-ALIPAY
					Vue.prototype.$statusBarHeight = res.statusBarHeight;
					Vue.prototype.$titleBarHeight = res.titleBarHeight;
					Vue.prototype.$customNavBarHeight = Vue.prototype.$statusBarHeight + Vue.prototype.$titleBarHeight;
					// #endif

					console.log('statusBarHeight=' + Vue.prototype.$statusBarHeight);
					console.log('titleBarHeight=' + Vue.prototype.$titleBarHeight);
					console.log('customNavBarHeight=' + Vue.prototype.$customNavBarHeight);

					vm.$store.commit('app/setUiInfo', {
						statusBarHeight: Vue.prototype.$statusBarHeight,
						titleBarHeight: Vue.prototype.$titleBarHeight,
						customNavBarHeight: Vue.prototype.$customNavBarHeight
					});
				}
			});
		}
	}
};
</script>

<style lang="scss">
/*每个页面公共css */
@import '@/components/uview-ui/index.scss';
@import url('~@/styles/colorui/main.css');
@import url('~@/styles/app.scss');
</style>
